package com.hzdl.car.contorlle;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.News;
import com.hzdl.car.bean.User;
import com.hzdl.car.service.NewsService;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.vo.NewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("news")
public class NewsControlle {

    @Autowired
    private NewsService newsService;
    @RequestMapping("init")
    public HzdlResultJson newsInit(NewsVo newsVo){
        PageInfo<News> pageInfo = newsService.findWhere(newsVo);
        return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("add")
    public HzdlResultJson add(NewsVo newsVo, HttpSession session){
        Object obj = session.getAttribute("session_user");
        User user=(User)obj;
        newsVo.setPublishername(user.getRealname());
        int row = newsService.add(newsVo);
        if (row>0){
            return new HzdlResultJson("添加成功",null);
        }else {
            return new HzdlResultJson(-1,"添加失败");
        }
    }

    @RequestMapping("update")
    public HzdlResultJson update(NewsVo newsVo,HttpSession session){

        Object obj = session.getAttribute("session_user");
        User user=(User)obj;
        newsVo.setPublishername(user.getRealname());
        int row = newsService.update(newsVo);
        if (row>0){
            return new HzdlResultJson("修改成功",null);
        }
        return new HzdlResultJson(-1,"修改失败");

    }

    @RequestMapping("delete")
    public HzdlResultJson delete ( NewsVo newsVo){
        int row = newsService.delete(newsVo.getId());
        if (row>0){
            return HzdlResultJson.DELETE_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_FAIL();
        }
    }

    @RequestMapping("deleteAll")
    public HzdlResultJson deleteAll ( @RequestParam(value = "ids[]") Integer[] ids){
        int row =0;
        for(Integer id:ids){
            row+=newsService.delete(id);
        }

        if (row>0){
            return HzdlResultJson.DELETE_ALL_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_ALL_FAIL();
        }
    }

    @RequestMapping("upload")
    public Map<String,Object> upload(MultipartFile file , HttpSession session  ){
        String originalFilename = file.getOriginalFilename();
        String path = null;

        Map<String,Object> map = new HashMap<>();
        try {

            path = ResourceUtils.getURL("classpath:").getPath() + "static/photo/" + originalFilename;
            File file1 = new File(path);
            if(!file1.getParentFile().exists()){
                file1.getParentFile().mkdirs();
            }
            file.transferTo(file1);
            map.put("code", 0);	//0表示上传成功
            map.put("msg","上传成功"); //提示消息
            Map<String,Object> map2 = new HashMap<String,Object>();
            map2.put("src", "/car/static/photo/"+originalFilename);
            map2.put("title", originalFilename);
            map.put("data", map2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            map.put("code", -1);	//0表示上传成功
            map.put("msg","上传失败"); //提示消息
            e.printStackTrace();
        }


        return map;
    }

    @RequestMapping("show")
    public HzdlResultJson show(){
        List<News> list = newsService.findList();
        return new HzdlResultJson(list);
    }

}
